package com.org.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.org.common.PageBean;
import com.org.common.Result;
import com.org.pojo.Collect;
import com.org.pojo.CommunityActivity;
import com.org.pojo.CommunityActivityRespon;
import com.org.service.ICommunityActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务表 前端控制器
 * </p>
 *
 * @author cjx
 * @since 2024-03-03
 */
@RestController
@RequestMapping("/community-activity")
public class CommunityActivityController {

    @Autowired
    private ICommunityActivityService communityActivityService;

    // 展示社区活动
    @GetMapping("/list")
    public Result<PageBean<CommunityActivity>> getCommunityActivities(
            Integer pageNum,    //第几页
            Integer pageSize,   //页面容量
            Boolean isUp,        //true：按创建时间升序排序，false：按创建时间降降序 不可为空
            String communityId,     //社区id 可为空，可不传
            String state     // 活动状态， 可为空，可不传
    ) {

        Page<CommunityActivity> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        LambdaQueryWrapper<CommunityActivity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (communityId != null && !communityId.isEmpty()) {
            lambdaQueryWrapper.eq(CommunityActivity::getCommunityId, communityId);
        }
        if (state != null && !state.isEmpty()) {
            lambdaQueryWrapper.eq(CommunityActivity::getState, state);
        }
        // isUp 为真，则按时间升序
        lambdaQueryWrapper.orderBy(isUp, true, CommunityActivity::getCreateTime);
        // 否则按时间降序
        lambdaQueryWrapper.orderBy(!isUp, false, CommunityActivity::getCreateTime);
        // 分页
        IPage<CommunityActivity> iPage = communityActivityService.page(page, lambdaQueryWrapper);

        PageBean<CommunityActivity> pageBean = new PageBean<>();
        pageBean.setTotal(iPage.getTotal());
        pageBean.setItems(iPage.getRecords());

        return Result.success(pageBean);
    }

    // 获取社区活动详细信息
    @GetMapping("/detail")
    public Result<CommunityActivity> getCommunityActivityDetail(long communityActivityId) {
        return Result.success(communityActivityService.getById(communityActivityId));
    }

    // 添加新社区活动
    @PostMapping("/add")
    public Result addCommunityActivity(@RequestBody CommunityActivity communityActivity) {
        communityActivity.setNumJoin(0);
        communityActivity.setCreateTime(LocalDateTime.now());
        // communityActivity.setState("已发布");
        return communityActivityService.save(communityActivity) ? Result.success(communityActivity) : Result.error("新增社区失败");
    }

    // 删除社区活动
    @GetMapping("/delete")
    public Result deleteCommunityActivity(long communityActivityId) {
        return communityActivityService.removeById(communityActivityId) ? Result.success() : Result.error("删除社区活动失败");
    }

    // 修改社区活动
    @PostMapping("/update")
    public Result updateCommunityActivity(@RequestBody CommunityActivity communityActivity) {
        return communityActivityService.updateById(communityActivity) ? Result.success(communityActivity) : Result.error("修改社区活动失败");
    }

    // 更新活动表的状态
    // 使用Spring的@Scheduled注解来定期执行此方法
    @Scheduled(fixedRate = 59000)  // 每59秒执行一次
    private void autoUpdateActivityState() {
        LocalDateTime now = LocalDateTime.now();

        LambdaUpdateWrapper<CommunityActivity> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1
                .set(CommunityActivity::getState, "进行中")
                .lt(CommunityActivity::getStartTime, now)  // startTime小于当前时间
                .gt(CommunityActivity::getEndTime, now);  // endTime大于当前时间
        communityActivityService.update(updateWrapper1);

        LambdaUpdateWrapper<CommunityActivity> updateWrapper2 = new LambdaUpdateWrapper<>();
        updateWrapper2
                .set(CommunityActivity::getState, "已完成")
                .lt(CommunityActivity::getEndTime, now);  // endTime小于当前时间
        communityActivityService.update(updateWrapper2);
    }


}
